package com.sss.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.sss.springcloud.common.Result;
import com.sss.springcloud.entities.Payment;
import com.sss.springcloud.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author sss
 * @date 2020-03-12 15:05
 **/
@RestController
@RequestMapping
public class OrderController {

    @Autowired
    private PaymentService paymentService;


    /**
     * 只配置fallback时，会处理java异常
     * 也即只会处理业务异常
    **/
    @GetMapping("/consumer/payment/fallback/{id}")
    @SentinelResource(value = "fallback1",fallback = "handlerException")
    public Result fallback(@PathVariable("id")Integer id){
        Result fallback = paymentService.fallback(id);
        if (id==5) {
            throw new IllegalArgumentException("参数不合法异常！！！！\t");
        }else if (id==6){
            throw new RuntimeException("运行时异常！！！\t");
        }
        return fallback;
    }

    public Result handlerException(Integer id, Throwable ex){
        return Result.fail("fallback*****服务器出错了，请稍后再试/(ㄒoㄒ)/~~！！");
    }


    /**
     * 只配置blockHandler时，不会处理java异常
     * 也即只会处理sentinel控制台定义的规则
     **/
    @GetMapping("/consumer/payment/blockHandler/{id}")
    @SentinelResource(value = "fallback2",blockHandler = "handlerBlockException")
    public Result fallback2(@PathVariable("id")Integer id){
        if (id==5) {
            throw new IllegalArgumentException("参数不合法异常！！！！\t");
        }else if (id==6){
            throw new RuntimeException("运行时异常！！！\t");
        }
        Payment payment = new Payment(211L, "fallbackTest\t");
        return Result.succes(payment);
    }

    public Result handlerBlockException(Integer id, BlockException ex){
        return Result.fail("handlerBlockException*****服务器被限流了，请稍后再试/(ㄒoㄒ)/~~！！");
    }

    /**
     * 同时配置blockHandler和fallback时，
     * 如果报的是java异常，则会走fallback方法
     * 如果报的是sentinel控制台定义的规则异常，则会走blockHandler方法
     * 如果同时报sentinel控制台定义的规则异常和java异常时，则会走blockHandler方法
     **/
    @GetMapping("/consumer/payment/blockHandlerAndFallback/{id}")
    @SentinelResource(value = "fallback3",fallback = "handlerException",blockHandler = "handlerBlockException")
    public Result fallback3(@PathVariable("id")Integer id){
        if (id==5) {
            throw new IllegalArgumentException("参数不合法异常！！！！\t");
        }else if (id==6){
            throw new RuntimeException("运行时异常！！！\t");
        }
        return paymentService.fallback(id);
    }


    /**
     * 加了exceptionsToIgnore后，如果抛出异常符合exceptionsToIgnore定义的值
     * 则不会执行fallback定义的方法
     * exceptionsToIgnore也即是忽略某些异常的处理
    **/
    @GetMapping("/consumer/payment/exceptionsToIgnore/{id}")
    @SentinelResource(value = "fallback4",fallback = "handlerException",blockHandler = "handlerBlockException",
                        exceptionsToIgnore = {IllegalArgumentException.class})
    public Result fallback4(@PathVariable("id")Integer id){
        if (id==5) {
            throw new IllegalArgumentException("参数不合法异常！！！！\t");
        }else if (id==6){
            throw new RuntimeException("运行时异常！！！\t");
        }
        Payment payment = new Payment(211L, "fallbackTest\t");
        return Result.succes(payment);
    }


}
